package wschat.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurationSupport;


/**
 * Created by alex on 15-1-23.
 */
@Configuration
public class WebSocketMessageBrokerConfig extends WebSocketMessageBrokerConfigurationSupport {
    

    
    @Override
    protected void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").withSockJS();
    }

//    @Override
//    protected void configureMessageBroker(MessageBrokerRegistry registry) {
//        registry.enableSimpleBroker("/queue/", "/topic/");
//        registry.setApplicationDestinationPrefixes("/app");
//    }


    @Override
    protected void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableStompBrokerRelay("/queue/", "/topic/")
                .setRelayHost("localhost")
                .setRelayPort(61613)
//                .setSystemLogin()
//                .setSystemPasscode()
//                .setClientLogin()
//                .setClientPasscode()
//                .setVirtualHost()
        ;
        registry.setApplicationDestinationPrefixes("/app");
    }


    @Bean
    public ChannelInterceptorAdapter sessionContextChannelInterceptorAdapter() {
        return new ChannelInterceptorAdapter() {
            @Override
            public Message<?> preSend(Message<?> message, MessageChannel channel) {
                return super.preSend(message, channel);
            }
        };
    }
}
